package com.dhwl.hsh.merchant.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 重写系统BaseAdapter
 * @author Derek.Kidd
 * @time 2016/4/23 13:29
 */
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter
{
    private List<T> list;
    
    protected Context context;
    
    public List<T> getList()
    {
        return list;
    }
    
    public void setList(List<T> list)
    {
        this.list = list;
    }
    
    public BaseAdapter(List<T> list)
    {
        init(null, list);
    }
    
    public BaseAdapter(Context context)
    {
        init(context, new ArrayList<T>());
    }
    
    public BaseAdapter(Context context, List<T> list)
    {
        init(context, list);
    }
    
    private void init(Context context, List<T> list)
    {
        this.list = list;
        this.context = context;
    }
    
    public void clear()
    {
        this.list.clear();
        notifyDataSetChanged();
    }
    
    public void addAll(List<T> list)
    {
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    
    @Override
    public int getCount()
    {
        return list == null ? 0 : list.size();
    }
    
    @Override
    public T getItem(int position)
    {
        return list.get(position);
    }
    
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    protected View inflate(int layoutResID, ViewGroup root)
    {
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(layoutResID, root, false);
        return view;
    }
    
}
